﻿using System;

using NUnit.Framework;

using OpenQA.Selenium;
using OpenQA.Selenium.Support.UI;
using System.Collections.ObjectModel;

namespace mvcForum.Tests.WebUI {

	[TestFixture]
	public class C002_CategoryTests {
		private IWebDriver driver;
		private const String categoryName = "My new category";
		private const Int32 sortOrder = 48;

		#region Initialization and cleanup
		[TestFixtureSetUp]
		public void TestInitialize() {
			driver = TestHelper.GetDriver();
		}

		[TestFixtureTearDown]
		public void TestCleanup() {
			if (driver != null) {
				driver.Close();
			}
		}
		#endregion

		[Test]
		public void EditCategory() {
			driver.LogOn(Constants.Users.Admin.Email, Constants.Users.Admin.Password);
			driver.VisitURL("forumadmin", "stats");

			IWebElement row = driver.FindBoardRows()[0];
			String boardName = row.FindElements(By.TagName("td"))[0].Text;

			IWebElement link = row.FindElement(By.LinkText("Edit"));
			link.Click();

			WebDriverWait wait = new WebDriverWait(driver, new TimeSpan(0, 0, 5));
			wait.Until(drv => drv.FindElement(By.Id("boardsec")));

			row = driver.FindCategoryRows()[0];
			String categoryName = row.FindElements(By.TagName("td"))[0].Text;

			link = row.FindElement(By.LinkText("Edit"));
			link.Click();


			// TODO:


			driver.LogOff();
		}

		[Test]
		public void CreateCategory() {
			driver.LogOn(Constants.Users.Admin.Email, Constants.Users.Admin.Password);
			driver.VisitURL("forumadmin", "stats");

			IWebElement row = driver.FindElement(By.Id("boards")).FindElements(By.XPath("table/tbody/tr"))[0];
			String boardName = row.FindElements(By.TagName("td"))[0].Text;
			row.FindElement(By.LinkText("Edit")).Click();

			WebDriverWait wait = new WebDriverWait(driver, new TimeSpan(0, 0, 10));
			wait.Until(drv => drv.FindElement(By.Id("boardsec")));

			try {
				// Create pop-up should be invisible!
				driver.FindElement(By.LinkText("Create"));
				Assert.AreEqual(false, true, "The create button was found");
			}
			catch {
				Assert.AreEqual(true, true, "No create button");
			}

			IWebElement addLink = driver.FindElement(By.LinkText("Add Category"));
			addLink.Click();

			try {
				// Create pop-up should be visible!
				driver.FindElement(By.LinkText("Create"));
				Assert.AreEqual(true, true, "The create button was found");
			}
			catch {
				Assert.AreEqual(false, true, "No create button found");
			}

			IWebElement closeLink = driver.FindElement(By.LinkText("Close"));
			closeLink.Click();

			try {
				// Create pop-up should be invisible!
				driver.FindElement(By.LinkText("Create"));
				Assert.AreEqual(false, true, "The create button was found");
			}
			catch {
				Assert.AreEqual(true, true, "No create button");
			}

			addLink.Click();

			IWebElement modal = driver.FindElement(By.Id("newcategorymodal"));

			IWebElement sortOrderField = modal.FindElement(By.Name("SortOrder"));
			driver.InsertText(sortOrderField, "");

			IWebElement createLink = driver.FindElement(By.LinkText("Create"));
			createLink.Click();

			Assert.AreEqual(true, driver.PageSource.Contains("The category name is required!"), "The error: \"category name required\" is shown");
			Assert.AreEqual(true, driver.PageSource.Contains("The sort order is required!"), "The error: \"sort order required\" is shown");
			Assert.AreEqual(false, driver.PageSource.Contains("A new category was created successfully!"), "The category was create without required values.");

			sortOrderField = modal.FindElement(By.Name("SortOrder"));
			driver.InsertText(sortOrderField, "d");
			createLink.Click();

			Assert.AreEqual(true, driver.PageSource.Contains("The sort order must be an integer!"), "The error: \"sort order integer\" is shown");

			closeLink = driver.FindElement(By.LinkText("Close"));
			closeLink.Click();

			addLink.Click();

			IWebElement nameField = modal.FindElement(By.Id("categoryname"));
			driver.InsertText(nameField, categoryName);
			sortOrderField = modal.FindElement(By.Name("SortOrder"));
			driver.InsertText(sortOrderField, sortOrder.ToString());

			createLink.Click();

			driver.Wait(2);

			Assert.AreEqual(true, driver.PageSource.Contains("A new category was created successfully!"), "Created successfully text displayed");

			driver.EditCategory(boardName, categoryName);

			Assert.AreEqual(categoryName, driver.GetTextBoxValueByName("Name"), "Category name");
			Assert.AreEqual(sortOrder.ToString(), driver.GetTextBoxValueByName("SortOrder"), "Sort order");

			driver.LogOff();
		}

		[Test]
		public void DeleteCategory() {
			driver.LogOn(Constants.Users.Admin.Email, Constants.Users.Admin.Password);
			driver.VisitURL("forumadmin", "stats");

			IWebElement row = driver.FindBoardRows()[0];
			String boardName = row.FindElement(By.TagName("td")).Text;
			row.FindElement(By.LinkText("Edit")).Click();

			WebDriverWait wait = new WebDriverWait(driver, new TimeSpan(0, 0, 10));
			wait.Until(drv => drv.FindElement(By.Id("boardsec")));

			try {
				// Create pop-up should be invisible!
				driver.FindElement(By.LinkText("OK"));
				Assert.AreEqual(false, true);
			}
			catch {
				Assert.AreEqual(true, true);
			}

			row = driver.FindCategoryRow(categoryName);
			IWebElement deleteLink = row.FindElement(By.LinkText("Delete"));
			deleteLink.Click();

			try {
				// Create pop-up should be invisible!
				driver.FindElement(By.LinkText("OK"));
				Assert.AreEqual(true, true);
			}
			catch {
				Assert.AreEqual(false, true);
			}

			IWebElement link = driver.FindElement(By.LinkText("OK"));
			link.Click();

			driver.EditBoard(boardName);

			Assert.AreEqual(null, driver.FindCategoryRow(categoryName), "Category deleted");

			driver.LogOff();
		}
	}
}